package org.liaohailong.victorlibrary

import android.util.Log
import org.liaohailong.library.RootApplication
import org.liaohailong.library.crash.CrashManager
import org.liaohailong.library.db.SQLite
import org.liaohailong.library.image.victor.Avatar
import org.liaohailong.library.log.LogConfig
import org.liaohailong.library.log.Logcat
import org.liaohailong.library.util.AppUtils
import org.liaohailong.victorlibrary.model.Student
import java.util.*

/**
 * Author: liaohailong
 * Date: 2019/3/12
 * Time: 23:15
 * Description:
 **/
class VictorApplication : RootApplication() {

    override fun onCreate() {
        super.onCreate()
        SQLite.init(this, "Victor", 1, true, Student::class.java)
        Avatar.INSTANCE.init(this, cacheDir.absolutePath)


        // log系统初始化
        val calendar = Calendar.getInstance()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)
        // log日志本地写入文件
        val logFilepath = "${externalCacheDir}/log/${year}-${month}-${day}.txt"
        val logConfig = LogConfig()
            .setLevel(Log.INFO)
            .setFilepath(logFilepath)
        Logcat.init(this, logConfig)
        Logcat.obtain(this).i("logFilepath = $logFilepath")

        // 全局java异常捕获
        val versionCode: String = java.lang.String.valueOf(AppUtils.getVersionCode(this))
        val versionName: String = AppUtils.getVersionName(this)
        Thread.setDefaultUncaughtExceptionHandler(CrashManager(this, versionCode, versionName))
    }
}